package com.bw.car_module.carlist.arouterservice;

import android.content.Context;
import androidx.room.Room;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bw.base_application_sdk.BaseApplication;
import com.bw.car_module.carlist.CartRespository;
import com.bw.car_module.carlist.dao.CartDataBase;
import com.bw.common.providerservice.CartApi;
import com.bw.network_sdk.BaseEntity;
import java.util.Map;
import io.reactivex.Observable;

@Route(path = "/car/cartapi" , name = "获取用户登录校验")
public class CartApiImpl implements CartApi {

    private CartRespository cartRespository;
    private CartDataBase dataBase;

    @Override
    public Observable<BaseEntity> insertCart(Map<String, Object> map) {
        long userId = (long) map.get("userId");
        //区分用户是否登录->登录添加到在线接口->未登录添加到离线数据库
        if (userId<=0)
            return cartRespository.insertCart(map,dataBase.dao());
        else
            return cartRespository.requestInsertCart(map);
    }

    @Override
    public void init(Context context) {
        dataBase = Room.databaseBuilder(BaseApplication.getInstance(),
                CartDataBase.class,"cart.db").build();
        cartRespository = new CartRespository();
    }
}
